﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SpaceInvaderz
{
    public class GameCore
    {
        public Page _page;
        public Point _worldLimits;
        public Sprite _hero;
        public Enviroment _enviroment;
        int incrementor;



        public GameCore(Page page)
        {
            this._page = page;
            Initialize();

        }

        public void Initialize()
        {
            _page.myCanvas.Children.Clear();
            _enviroment = new Enviroment(this);
            Sprite b = new Sprite(700, 1400, @"SpaceInvaderz;component/Images/Starfield.png", _page);
            _enviroment.Spawn();
            _enviroment.Spawn();
            _enviroment.Spawn();
            _enviroment.Spawn();
            _enviroment.Spawn();
            _enviroment.Spawn();

            _hero = new Sprite(30, 30, @"SpaceInvaderz;component/Images/MrBlue.png", _page);
            _hero._shield = new Sprite(70, 70, @"SpaceInvaderz;component/Images/LittleShield.png", _page);
            _hero._shield.VelocityX = _hero.VelocityX;
            _hero._shield.VelocityY = _hero.VelocityY;
            _hero._shield.LocationX = _hero.LocationX;
            _hero._shield.LocationY = _hero.LocationY;
            _hero._shield._visual.SetValue(Canvas.LeftProperty, _hero.LocationX);
            _hero._shield._visual.SetValue(Canvas.TopProperty, _hero.LocationY);
            _hero.Hero = true;
            _hero.LocationX = 1;
            _hero.LocationY = 1;
            _hero._visual.SetValue(Canvas.LeftProperty, _hero.LocationX);
            _hero._visual.SetValue(Canvas.TopProperty, _hero.LocationY);
            _worldLimits = _page.WorldLimits;
            Speed = 120;
            incrementor = 500;

        }

        public void MainLoop(double gameTime, double lastTime)
        {
            UpdateHero(gameTime, lastTime);
            _enviroment.Update(gameTime, lastTime);
            Points++;
            PowerUps();
            //_page.HpBox.Text = _hero.Hp.ToString();
            //_page.ShieldBox.Text = _hero._shield.Hp.ToString();
        }

        private void PowerUps()
        {
            Random l = new Random();
            Random n = new Random(l.Next());
            Random z = new Random(n.Next());
            Sprite pwr;
            if (incrementor <= 0)
            {
                int k = l.Next(100);
                if (k >= 66)
                {
                    _enviroment.SpeedPack(n.Next((int)_page.WorldLimits.X - 20), z.Next((int)_page.WorldLimits.Y - 20));
                }
                else if (k <= 33)
                {
                    _enviroment.HealthPack(n.Next((int)_page.WorldLimits.X - 20), z.Next((int)_page.WorldLimits.Y - 20));
                }
                else
                {
                    _enviroment.ShieldPack(n.Next((int)_page.WorldLimits.X - 20), z.Next((int)_page.WorldLimits.Y - 20));
                }



                incrementor = 700;
            }
            else
                incrementor--;
        }

        void UpdateHero(double gameTime, double lastTime)
        {
            _hero.Update(gameTime, lastTime);

            if (_hero.Hp < 0)
            {
                _page.myCanvas.Children.Clear();
                Initialize();
            }



            foreach (Sprite e in _enviroment.Enemies)
            {
                if (Sprite.Collision(e, _hero._shield) && _hero._shield.Hp > 0)
                {
                    _hero._shield.Hp -= e.Attack;
                    e.Hp = 0;
                    return;
                }

                else if (Sprite.Collision(e, _hero))
                {

                    _hero.Hp -= e.Attack;
                    e.Hp = 0;
                    return;
                }
            }
        }

        public int Points
        {
            get;
            set;
        }

        public double Speed
        {
            get;
            set;
        }



    }
}
